<?php

namespace Admin\Controller;
use Think\Controller;

class DepartmentController extends CommonController
{
    /**
     *
     */
    public function cate_depart()
    {
        if (IS_POST) {
            //$post=I('post.');
            //dump($post);die();
            //通过数据对象获取数据
            //	$model=new  Admin\Model\DepartmentModel();
            $model = D('Department');
            $data = $model->create();
            //判断验证结果
            if (!$data) {
                $this->error($model->getError());
                exit;
            }
            $res = $model->add();

            if ($res) {
                $this->success('添加成功', U('cate_depart'), 3);
            } else {
                $this->error('添加失败');
            }
        } else {
            //查出高级部门
            $sql = "SELECT A.*, B.depart_name AS pdepart_name 
					FROM  `department` AS A
						LEFT JOIN department AS B ON A.depart_pid = B.depart_id";
            $data = M('Department')->query($sql);
            //$model=M('Department');
            //$data=$model->query($sql);
            /*
            foreach ($data as $key => $value){
                //查询部门ID>0的，并设定上级部门名称为pdepart_name
                if($value['depart_pid']>0){
                   $temp=$model->find($value['depart_pid']);
                   $data[$key]['pdepart_name'] = $temp['depart_name'];
                }
                //给最高权力机构设置名称
                if($value['depart_pid']==0){
                   $data[$key]['pdepart_name'] = '顶级部门';
                }
            }
            */
            $data = getSubTree($data);//按层级排序
            $this->assign('data', $data);
            $this->display('cate_depart');

        }
        //$data=getSubTree($data);
    }
}
?>